package com.hnu.store.controller;

import com.hnu.store.entity.Inform;
import com.hnu.store.service.IInformService;
import com.hnu.store.utils.JsonResult;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * BaseController用于处理异常
 */
@RestController
@CrossOrigin

@RequestMapping("inform")
public class InformController extends BaseController {

  @Autowired
  private IInformService iInformService;

  /**
   * 根据用户名获取聊天记录
   * @param userName 己方用户名
   * @param otherName 对方用户名
   * @return json数据
   */

  @RequestMapping("getMessageListByName")
  public JsonResult getMessageListByName(String userName, String otherName) {
    JsonResult result = new JsonResult(OK);
    List<Inform> informList = iInformService.getMessageListByName(userName, otherName);
    result.addData("informList", informList);
    return result;
  }

  /**
   * 根据用户名获取session
   * @param userName 用户名
   * @return json数据
   */
  @RequestMapping("getSessionByName")
  public JsonResult getSessionByName(String userName){
    Set<String> session=iInformService.getSessionListByName(userName);
    JsonResult result=new JsonResult(OK);
    result.addData("sessionList",session);
    return result;
  }

  /**
   * 发送消息
   * @param sendName 发送者
   * @param receiveName 接收者
   * @param content 内容
   * @return json
   */
  @RequestMapping("addMessage")
  public JsonResult addMessage(String sendName,String receiveName,String content){
    Inform inform=iInformService.addMessage(sendName,receiveName,content);
   JsonResult result= new JsonResult(OK);
   result.addData("inform",inform);
   return result;
  }
}
